#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@author: liang kang
@contact: gangkanli1219@gmail.com
@time: 2018/5/24 12:09
@desc: 
"""
import argparse
from pathlib import Path

from nodule.nodule_reader import NoduleReader
from utils.config import Files
from utils.file.data_io import NoduleListWriter
from utils.log import get_file_logger
from utils.scan_finder import ScanFinder


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--root', dest='root', type=str,
                        default=Files.ORIGIN_ROOT)
    parser.add_argument('--output', dest='output', type=str,
                        default=Files.NODULE_ROOT)
    return parser.parse_args()


def prepare_nodule(params):
    reader = ScanFinder(params.root, logger=logger)
    path = Path(params.output)
    if not path.exists():
        path.mkdir()
    this_file = Path(__file__).absolute().parent.parent
    for file in reader:
        if file:
            logger.info(str(Path(file['dcm']).relative_to(this_file)))
            nodule_reader = NoduleReader(**file)
            nodule_reader.update()
            if nodule_reader.break_scan:
                logger.info(nodule_reader.msg)
                continue
            writer = NoduleListWriter(nodule_reader.nodule_list)
            writer.update()


if __name__ == '__main__':
    ARGS = parse_args()
    logger = get_file_logger('数据预处理',
                             str(ARGS.output) + '/output.log', 1)
    prepare_nodule(ARGS)
